﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Data;

namespace AcadPosApp.Relatorios
{
    public partial class RelatorioGestorPolo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                carregaEstados();
            }
        }

        private void carregaEstados()
        {
            ddlEstadoPesquisa.DataSource = ControlClass.CNEstado.listaTodosEstados();
            ddlEstadoPesquisa.DataTextField = "ESTADO";
            ddlEstadoPesquisa.DataValueField = "ID_ESTADO";
            ddlEstadoPesquisa.DataBind();
            ddlEstadoPesquisa.Items.Insert(0, new ListItem("Todos", ""));

        }

        protected void btnImprimirRelatorio_Click(object sender, EventArgs e)
        {
            int idEstado = 0;
            if (ddlEstadoPesquisa.SelectedValue != "")
            {
                idEstado = Convert.ToInt32(ddlEstadoPesquisa.SelectedValue);
            }

            DataTable dtImp = ControlClass.CNPolo.relGestorPolo(idEstado);
            if (dtImp.Rows.Count == 0)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", "bootbox.alert('Não há registros à serem impressos!', function() {});", true);
            }
            else
            {

                ReportViewer ReportViewer1 = new ReportViewer();
                ReportViewer1.Reset();
                ReportViewer1.ProcessingMode = ProcessingMode.Local;
                ReportViewer1.LocalReport.ReportPath = Server.MapPath(@"~/Relatorios/relGestorPolo.rdlc");
                ReportViewer1.LocalReport.DataSources.Clear();

                ReportDataSource rds1 = new ReportDataSource();
                rds1.Name = "DataSet1";
                rds1.Value = dtImp;

                ReportDataSource rds2 = new ReportDataSource();
                rds2.Name = "DataSet2";
                rds2.Value = ControlClass.CNParametros.getCabecalhoRelatorio();

                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.DataSources.Add(rds1);
                ReportViewer1.LocalReport.DataSources.Add(rds2);
                ReportViewer1.LocalReport.Refresh();

                #region Gerar PDF
                Warning[] warn = null;
                string[] streamids = null;
                string mimeType = "application/pdf";
                string encoding = string.Empty;
                string extension = string.Empty;
                byte[] byteViewer;

                byteViewer = ReportViewer1.LocalReport.Render("pdf", null, out mimeType, out encoding, out extension, out streamids, out warn);
                Response.Buffer = true;
                Response.Clear();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment; filename=GestorPolo.pdf");
                Response.BinaryWrite(byteViewer);
                Response.Flush();

                Response.End();
                Response.ClearContent();
                #endregion
            }
        }
    }
}